package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-23 20:32
 */
@RestController
@Slf4j
@Api(tags = "管理端订单管理接口")
@RequestMapping("/admin/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 根据Id查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> findByorderId(@PathVariable Long id) {
        log.info("根据Id查询订单详情:{}", id);
        OrderVO orderVO = ordersService.findByorderId(id);
        return Result.success(orderVO);
    }

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTOP
     * @return
     */
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> pageOrder(OrdersPageQueryDTO ordersPageQueryDTOP) {
        log.info("订单搜索:{}", ordersPageQueryDTOP);
        PageResult pageResult = ordersService.pageadmin(ordersPageQueryDTOP);
        return Result.success(pageResult);
    }

    /**
     * 个状态数量的统计
     *
     * @return
     */
    @ApiOperation("个状态数量的统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics() {
        log.info("个状态数量的统计");
        OrderStatisticsVO orderStatisticsVO = ordersService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 接单
     *
     * @param ordersConfirmDTO
     * @return
     */
    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接单:{}", ordersConfirmDTO);
        ordersService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @param ordersRejectionDTO
     * @return
     */
    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("拒绝订单接口:{}", ordersRejectionDTO);
        Orders order = new Orders();
        BeanUtils.copyProperties(ordersRejectionDTO, order);
        ordersService.updateid(order);
        return Result.success();
    }

    /**
     * 取消订单
     *
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("取消订单接口:{}", ordersCancelDTO);
        Orders order = new Orders();
        BeanUtils.copyProperties(ordersCancelDTO, order);
        ordersService.updateid(order);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result deliveryId(@PathVariable Long id){
        log.info("派送订单", id);
        ordersService.deliveryId(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @ApiOperation("完成订单")
    @PutMapping("complete/{id}")
    public  Result  completeid(@PathVariable Long id){
        log.info("完成订单");
        ordersService.complete(id);
        return Result.success();
    }
}